import { IMovie } from "../../typings/interface";
import { movieActionEnums } from "../typings/movieEnums";
import { IMovieState } from "../typings/movieInterface";
import * as actions from "../typings/movieTypes";
// 初始状态
const initMovieState:IMovieState = {
  list:[],
  conditions:{
    page:1,
    limit:10,
    keyWord:''
  },
  isLoading:false,
  total:0
}
// movieReducer
export const movieReducer = (state:IMovieState = initMovieState, action:actions.movieActions):IMovieState => {
  const {type, payload} = action
  switch (type) {
    case movieActionEnums.SAVE_MOVIE:
      state.list = payload
      state.total = state.list.length
      return {...state}
    case movieActionEnums.ADD_MOVIE:
      state.list = [...state.list, payload]
      state.total = state.list.length
      return {...state}
    case movieActionEnums.DEL_MOVIE:
      const delIndex = state.list.findIndex(item=>(item.id === payload))
      state.list.splice(delIndex,1)
      state.total = state.list.length
      return state
    case movieActionEnums.SET_LOADING:
      state.isLoading = payload
      return {...state}
    case movieActionEnums.SET_CONDITION:
      state.conditions = {...state.conditions, ...payload}
      return {...state}
    default:
      return state
  }
}